//
//  MiddleManAddLatentCustomerViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/4/29.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//  经纪人－－添加真实客户客户

#import "MiddleManAddLatentCustomerViewController.h"
#import "UIView+circular.h"
#import "BrokerDataMange.h"
#import "UserModal.h"
#import "AddRealCusModel.h"

#import "MBProgressHUD+MJ.h"
#import "config.h"
#import "LoginDataMange.h"
#import "StringTool.h"
#import "UserModal.h"

#import "BingPremiseVc.h"
@interface MiddleManAddLatentCustomerViewController ()<UITextFieldDelegate,BingPremiseVcDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *mainView;
@property (strong, nonatomic) BrokerDataMange *dataMange;
@property (strong, nonatomic) AddRealCusModel *model;
@property (strong, nonatomic) LoginDataMange *cpathaDM;//短信验证码


@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UIView *view4;
@property (weak, nonatomic) IBOutlet UIView *view5;
@property (weak, nonatomic) IBOutlet UIView *view6;


@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *yanZhengCodeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *repeatPwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *siteTextField;

- (IBAction)clickBtn:(UIButton *)sender;



@end

@implementation MiddleManAddLatentCustomerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    _siteTextField.text = [UserModal shareUserModel].prmisedNames;
}

-(BrokerDataMange *)dataMange
{
    if (!_dataMange) {
        _dataMange = [[BrokerDataMange alloc]init];
    }
    return _dataMange;
}

-(AddRealCusModel *)model
{
    if (!_model) {
        _model = [[AddRealCusModel alloc]init];
    }
    return _model;
}

-(LoginDataMange *)cpathaDM
{
    if (!_cpathaDM) {
        _cpathaDM = [[LoginDataMange alloc]init];
    }
    return _cpathaDM;
}
-(void)initView
{
    [self.titleView setMianTitle:@"添加客户"];
    self.titleView.doneBtn.hidden = YES;
    
    [self.view1 viewToCircular];
    [self.view2 viewToCircular];
    [self.view4 viewToCircular];
    [self.view5 viewToCircular];
    [self.view6 viewToCircular];
    
    self.siteTextField.enabled = NO;
}
- (IBAction)clickBtn:(UIButton *)sender
{
    NSInteger tag = sender.tag;
    switch (tag) {
        case 0://验证码
        {
            if([StringTool isBlankString:_phoneTextField.text])
                [MBProgressHUD showError:@"客户手机号为空!"];
            else
            [self getCpatch];
        }
        
            break;
        case 1://绑定楼盘
        {
            BingPremiseVc *vc = [[BingPremiseVc alloc]initWithNibName:@"BingPremiseVc" bundle:nil];
            vc.coustomerCode = [UserModal shareUserModel].userCode;
            vc.vcState = PREVIOUSVC_ADDCUS;
            vc.delegate = self;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2://保存信息
            if ([self checkInfo])
            {
                [self updata];
            }
            
            break;
            
        default:
            break;
    }
}
#pragma mark 检查输入信息
-(BOOL)checkInfo
{
    if ([StringTool isBlankString:_nameTextField.text])
    {
        [MBProgressHUD showError:@"客户姓名为空!"];
        return NO;
    }else if ([StringTool isBlankString:_phoneTextField.text])
    {
        [MBProgressHUD showError:@"客户手机号为空!"];
        return NO;
    }else if (![StringTool isBlankString:_phoneTextField.text])
    {
        if (![self validateMobile:_phoneTextField.text]) {
            [MBProgressHUD showError:@"手机号码不正确!"];
            return NO;
        }
    }
//    if ([StringTool isBlankString:_yanZhengCodeTextField.text])
//    {
//        [MBProgressHUD showError:@"验证码为空!"];
//        return NO;
//    }else if ([StringTool isBlankString:_pwdTextField.text])
//    {
//        [MBProgressHUD showError:@"请输入密码!"];
//        return NO;
//    }else if ([StringTool isBlankString:_repeatPwdTextField.text])
//    {
//        [MBProgressHUD showError:@"请再次输入密码!"];
//        return NO;
//    }else if (![_repeatPwdTextField isEqual:_pwdTextField])
//    {
//        [MBProgressHUD showError:@"两次密码输入不一致!"];
//        return NO;
//    }else if ([StringTool isBlankString:_siteTextField.text])
//    {
//        self.model.premisesIds = @"";
//    }else
//    {
//        self.model.premisesIds = _siteTextField.text;
//    }
    self.model.premisesIds = _siteTextField.text;
    self.model.name = _nameTextField.text;
    self.model.mobile = _phoneTextField.text;
    self.model.captch = _yanZhengCodeTextField.text;
    self.model.password = _pwdTextField.text;
    self.model.repassword = _repeatPwdTextField.text;
    
   
    return YES;
}
#pragma mark 获取短信验证码
-(void)getCpatch
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [self.cpathaDM captchaWithMobile:self.phoneTextField.text];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                [MBProgressHUD showSuccess:@"验证码已发送，注意查收"];
            }
        });
    });
}

#pragma mark 提交数据
-(void)updata
{
    [MBProgressHUD showMessage:@"请操作" toView:self.view];
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *customerId = [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
        ResulteMod *mod = [bself.dataMange addconsumerWithCustmerModel:bself.model withCustomerId:customerId withpotentialId:nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:bself.view animated:YES];
            
            if (mod.resltState == ResultFail) {
                
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                [MBProgressHUD showSuccess:@"添加成功"];
                [bself.nav popViewControllerAnimated:YES];
            }
            
        });
    });
}




- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
    CGRect dd =  [textField.superview convertRect:textField.frame toView:self.view];
    
    int tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-[UserModal shareUserModel].keyboardHeight-tvbotY;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.view.frame;
        buf.origin.y = up ? mid : 0;
        [self.view setFrame:buf];
        
        [UIView commitAnimations];
    }else if(up==false){
        
        [self viewRest];
    }
}



-(void)viewRest{
    
    
    const float movementDuration = 0.3f;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    
    CGRect buf = self.view.frame;
    buf.origin.y =  0;
    [self.view setFrame:buf];
    
    [UIView commitAnimations];
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    
    [self viewRest];
    return  YES;
}

- (void)backAction
{
    [self.nav popViewControllerAnimated:YES];
}

@end
